package org.dxy.infrastructure.persistent.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.query.MPJLambdaQueryWrapper;
import org.dxy.infrastructure.persistent.mapper.WorkstationViewMapper;
import org.dxy.infrastructure.persistent.po.query.ProductViewPo;
import org.dxy.infrastructure.persistent.po.query.WorkstationBasicViewPo;
import org.dxy.infrastructure.persistent.service.WorkstationBasicViewService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 工位统一视图服务实现
 * @author dxy
 * @createTime 2025/10/13
 */
@Service
public class WorkstationBasicViewServiceImpl extends MPJBaseServiceImpl<WorkstationViewMapper, WorkstationBasicViewPo>
        implements WorkstationBasicViewService {

    @Override
    public Page<WorkstationBasicViewPo> pageWorkstationView(Page<WorkstationBasicViewPo> page,
                                                             Long factoryId,
                                                             Long workshopId,
                                                             Long productionLineId,
                                                             String workstationCode,
                                                             String workstationName,
                                                             String status) {
        LambdaQueryWrapper<WorkstationBasicViewPo> wrapper = new LambdaQueryWrapper<>();
        
        if (factoryId != null) {
            wrapper.eq(WorkstationBasicViewPo::getFactoryId, factoryId);
        }
        if (workshopId != null) {
            wrapper.eq(WorkstationBasicViewPo::getWorkshopId, workshopId);
        }
        if (productionLineId != null) {
            wrapper.eq(WorkstationBasicViewPo::getProductionLineId, productionLineId);
        }
        if (workstationCode != null && !workstationCode.trim().isEmpty()) {
            wrapper.like(WorkstationBasicViewPo::getWorkstationCode, workstationCode);
        }
        if (workstationName != null && !workstationName.trim().isEmpty()) {
            wrapper.like(WorkstationBasicViewPo::getWorkstationName, workstationName);
        }
        if (status != null && !status.trim().isEmpty()) {
            wrapper.eq(WorkstationBasicViewPo::getStatus, status);
        }
        
        wrapper.orderByDesc(WorkstationBasicViewPo::getCreatedTime);
        
        return page(page, wrapper);
    }

    @Override
    public WorkstationBasicViewPo getWorkstationViewById(Long workstationId) {
        if (workstationId == null) {
            return null;
        }
        return getById(workstationId);
    }

    @Override
    public List<WorkstationBasicViewPo> listWorkstationViewByProductionLineId(Long productionLineId) {
        if (productionLineId == null) {
            return List.of();
        }
        return list(new LambdaQueryWrapper<WorkstationBasicViewPo>()
                .eq(WorkstationBasicViewPo::getProductionLineId, productionLineId)
                .orderByDesc(WorkstationBasicViewPo::getCreatedTime));
    }

    @Override
    public List<WorkstationBasicViewPo> listAllWorkstationView() {
        return list(new LambdaQueryWrapper<WorkstationBasicViewPo>()
                .orderByDesc(WorkstationBasicViewPo::getCreatedTime));
    }

    @Override
    public WorkstationBasicViewPo getByCode(String workstationCode) {
        MPJLambdaQueryWrapper<WorkstationBasicViewPo> lambdaQueryWrapper = new MPJLambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(WorkstationBasicViewPo::getWorkstationCode, workstationCode);
        return this.getOne(lambdaQueryWrapper);
    }

    @Override
    public Long findIdByCode(String workstationCode) {
        MPJLambdaQueryWrapper<WorkstationBasicViewPo> lambdaQueryWrapper = new MPJLambdaQueryWrapper<>();
        lambdaQueryWrapper.select(WorkstationBasicViewPo::getId)
                .eq(WorkstationBasicViewPo::getWorkstationCode, workstationCode);
        Long id = this.selectJoinOne(Long.class,lambdaQueryWrapper);

        if (id == null) {
            throw new IllegalArgumentException("工位编码不存在：" + workstationCode);
        }
        return id;
    }
}